﻿Public Class frmChuyenMon

    Sub LoadDanhSach()
        Dim _dt As New DataTable
        _dt = ChuyenMonDAO.SelectAll()
        dgvTTChuyenMon.DataSource = _dt
    End Sub

    Sub Display()
        If (dgvTTChuyenMon.SelectedRows.Count > 0) Then
            txtMACHUYENMON.Text = dgvTTChuyenMon.SelectedRows(0).Cells(0).Value.ToString()
            txtTENCHUYENMON.Text = dgvTTChuyenMon.SelectedRows(0).Cells(1).Value.ToString()
        End If
    End Sub

    Private Sub frmChuyenMon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvTTChuyenMon.Rows.Clear()
        LoadDanhSach()
    End Sub

    Private Sub dgvTTChuyenMon_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTChuyenMon.CellContentClick
        Display()
    End Sub

    Private Sub frmChuyenMon_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTChuyenMon.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _cm As ChuyenMonDTO = New ChuyenMonDTO()
                _cm.MaChuyenMon = txtMACHUYENMON.Text
                _cm.TenChuyenMon = txtTENCHUYENMON.Text
                Try
                    ChuyenMonDAO.Update(_cm)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub dgvTTChuyenMon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTChuyenMon.SelectionChanged
        Display()
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _cm As ChuyenMonDTO = New ChuyenMonDTO()
        _cm.MaChuyenMon = txtMACHUYENMON.Text
        Try
            ChuyenMonDAO.Delete(_cm)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        MessageBox.Show("Xóa dữ liệu thành công.")
        LoadDanhSach()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTChuyenMon.SelectedRows.Count > 0) Then
            Dim _cm As ChuyenMonDTO = New ChuyenMonDTO()
            _cm.MaChuyenMon = txtMACHUYENMON.Text
            _cm.TenChuyenMon = txtTENCHUYENMON.Text
            Try
                ChuyenMonDAO.Update(_cm)
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            MessageBox.Show("Cập nhật dữ liệu thành công.")
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        Dim _cm As ChuyenMonDTO = New ChuyenMonDTO()
        _cm.TenChuyenMon = txtTENCHUYENMON.Text
        Try
            ChuyenMonDAO.Insert(_cm)
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        MessageBox.Show("Thêm dữ liệu thành công.")
        LoadDanhSach()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub
End Class